<?php

include_once MODELS."loja.php";
include_once MODELS."cargo.php";
include_once MODELS."cidade.php";
include_once MODELS."perfil.php";

class FuncionarioModel extends Database {
    
    const TABLE = "funcionario";
    
    const ID = "id";
    const NOME = "nome_funcionario";
    const LOGIN = "login_sistema";
    const SENHA = "senha";
    const NASCIMENTO = "data_nascimento";
    const ADMISSAO = "data_admissao";
    const DEMISSAO = "data_demissao";
    const CIDADE = "cidade";
    const RUA = "rua";
    const NUMERO = "numero";
    const BAIRRO = "bairro";
    const CEP = "cep";
    const CPF = "cpf";
    const RG = "rg";
    const CPT = "cpt";
    const REFERENCIA = "referencia";
    const BANCO = "banco";
    const AGENCIA = "agencia";
    const CONTA = "conta";
    const CARGO = "id_funcionario_cargo";
    const LOJA = "id_loja";
    const PERFIL = "id_perfil_sistema";
    const EMAIL = "email";
    
    public function insert(Funcionario $funcionario) {
        $fields = array(self::NOME,self::LOGIN,self::SENHA, self::NASCIMENTO,
                        self::ADMISSAO, self::DEMISSAO,    
                        self::CIDADE, self::RUA, self::NUMERO,
                        self::BAIRRO, self::CEP, self::CPF, self::RG, self::CPT,
                        self::REFERENCIA, self::BANCO, self::AGENCIA, self::CONTA,
                        self::CARGO, self::LOJA, self::PERFIL, self::EMAIL);
        $vars = get_object_vars($funcionario);
        unset($vars["id"]);
        unset($vars["telefones"]);
        $values = Database::turnInValues($vars);
        $res = parent::insert(self::TABLE, implode(",", $fields), $values);
        if($res) return $this->getAnnalisses()->lastInseredtId();
        return null;
    }
    
    public function select($fields = " * ", $condition = null, $limit = null) {
        if(is_array($fields)) $fields = implode (",", $fields);
        $res = parent::select(self::TABLE, $fields, $condition, $limit);
        $ana = $this->getAnnalisses();
        $funcionarios = array();
        while(($row = $ana->fetchObject($res)) !== false){
            $funcionarios[] = new Funcionario(
                                isset($row->{ self::ID })? $row->{ self::ID } : 0,
                                isset($row->{ self::NOME })? $row->{ self::NOME } : "",
                                isset($row->{ self::LOGIN })? $row->{ self::LOGIN } : "",
                                isset($row->{ self::SENHA })? $row->{ self::SENHA } : "",
                                isset($row->{ self::NASCIMENTO })? $row->{ self::NASCIMENTO } : "",
                                isset($row->{ self::ADMISSAO })? $row->{ self::ADMISSAO } : "",
                                isset($row->{ self::DEMISSAO })? $row->{ self::DEMISSAO } : "",
                                isset($row->{ self::CIDADE })? $row->{ self::CIDADE } : 0,
                                isset($row->{ self::RUA })? $row->{ self::RUA } : "",
                                isset($row->{ self::NUMERO })? $row->{ self::NUMERO } : "",
                                isset($row->{ self::BAIRRO })? $row->{ self::BAIRRO } : "",
                                isset($row->{ self::CEP })? $row->{ self::CEP } : "",
                                isset($row->{ self::CPF })? $row->{ self::CPF } : "",
                                isset($row->{ self::RG })? $row->{ self::RG } : "",
                                isset($row->{ self::CPT })? $row->{ self::CPT } : "",        
                                isset($row->{ self::REFERENCIA })? $row->{ self::REFERENCIA } : "",
                                isset($row->{ self::BANCO })? $row->{ self::BANCO } : "",
                                isset($row->{ self::AGENCIA })? $row->{ self::AGENCIA } : "",
                                isset($row->{ self::CONTA })? $row->{ self::CONTA } : "",
                                isset($row->{ self::CARGO })? $row->{ self::CARGO } : "",
                                isset($row->{ self::LOJA })? $row->{ self::LOJA } : "",
                                isset($row->{ self::PERFIL })? $row->{ self::PERFIL } : "",
                                isset($row->{ self::EMAIL}) ? $row->{ self::EMAIL} : ""        
                                );
        }
        return $funcionarios;
    }
    
    public function superSelect($condition = null, $limit = null){
        $fields = array(self::TABLE.".".self::ID, self::NOME,self::LOGIN,self::SENHA,
                        self::TABLE.".".self::RUA, self::DEMISSAO, self::ADMISSAO,
                        self::TABLE.".".self::NUMERO, CidadeModel::NOME, self::TABLE.".".self::BAIRRO,self::CPF,
                        self::NASCIMENTO, self::TABLE.".".self::CEP, self::RG, self::CPT,
                        self::REFERENCIA,self::BANCO,self::AGENCIA,self::CONTA,
                        CargoModel::NOME, LojaModel::SIGLA, PerfilModel::NOME, self::EMAIL);
        
        $fields_joined = implode(",", $fields);
        
        $joins[] = "LEFT JOIN ".CargoModel::TABLE." ON ".self::TABLE.".".self::CARGO." = ". CargoModel::TABLE.".".CargoModel::ID;
        $joins[] = "LEFT JOIN ".LojaModel::TABLE." ON ".self::TABLE.".".self::LOJA." = ".LojaModel::TABLE.".".LojaModel::ID;
        $joins[] = "LEFT JOIN ".PerfilModel::TABLE." ON ".self::PERFIL." = ".PerfilModel::TABLE.".".PerfilModel::ID;
        $joins[] = "LEFT JOIN ".CidadeModel::TABLE." ON ".self::TABLE.".".self::CIDADE." = ".CidadeModel::TABLE.".".CidadeModel::ID;
        
        $res = parent::select(self::TABLE." ".implode(" ", $joins), $fields_joined, $condition, $limit);
        $ana = $this->getAnnalisses();
        $funcionarios = array();
        while(($row = $ana->fetchObject($res)) !== false){
            $funcionarios[] = new Funcionario(
                                $row->{ self::ID },
                                $row->{ self::NOME },
                                $row->{ self::LOGIN },
                                $row->{ self::SENHA },
                                $row->{ self::NASCIMENTO},
                                $row->{ self::ADMISSAO},
                                $row->{ self::DEMISSAO},
                                $row->{ CidadeModel::NOME},       
                                $row->{ self::RUA },
                                $row->{ self::NUMERO },
                                $row->{ self::BAIRRO },
                                $row->{ self::CEP },
                                $row->{ self::CPF },
                                $row->{ self::RG },
                                $row->{ self::CPT },        
                                $row->{ self::REFERENCIA },
                                $row->{ self::BANCO },
                                $row->{ self::AGENCIA },
                                $row->{ self::CONTA },
                                $row->{ CargoModel::NOME },
                                $row->{ LojaModel::SIGLA },
                                $row->{ PerfilModel::NOME },
                                $row->{ self::EMAIL }        
                                );
        }
        return $funcionarios;
    }
    
    public function update(Funcionario $funcionario) {
        $dic = array (
            self::NOME => $funcionario->nome,
            self::LOGIN => $funcionario->login,
            self::SENHA => $funcionario->senha,
            self::NASCIMENTO => $funcionario->nascimento,
            self::ADMISSAO => $funcionario->admissao,
            self::DEMISSAO => $funcionario->demissao,
            self::CIDADE => $funcionario->cidade,
            self::RUA => $funcionario->rua,
            self::NUMERO => $funcionario->numero,
            self::BAIRRO => $funcionario->bairro,
            self::CEP => $funcionario->cep,
            self::CPF => $funcionario->cpf,
            self::RG => $funcionario->rg,
            self::CPT => $funcionario->cpt,
            self::REFERENCIA => $funcionario->referencia,
            self::BANCO => $funcionario->banco,
            self::AGENCIA => $funcionario->agencia,
            self::CONTA => $funcionario->conta,
            self::CARGO => $funcionario->cargo,
            self::LOJA => $funcionario->loja,
            self::PERFIL => $funcionario->perfil,
            self::EMAIL => $funcionario->email
        );
        $condition = self::ID." = ".$funcionario->id;
        return $this->formattedUpdates(self::TABLE, Database::turnInUpdateValues($dic), $condition);
    }
    
    public function delete($func_id) {
        $condition = self::ID." = $func_id";
        return parent::delete(self::TABLE, $condition);
    }
    
    public function handleError() {
        $err_code = $this->errorCode(); 
        if($err_code == Annalisses::ERR_DUPLICATE) 
            return "Já existe um funcionário com o mesmo CPF, RG ou EMAIL";
        return parent::handleError();
    }
    
}
?>
